package ContentManagers.Security;

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
 
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.SecureRandom;
import java.util.ResourceBundle;
 
public abstract class Encryptor {
 
	
        public static String encrypt(String text) throws Exception {
            BASE64Decoder decoder = new BASE64Decoder();
            ResourceBundle bundle = ResourceBundle.getBundle("key");
            byte[] raw = decoder.decodeBuffer(bundle.getString("key"));
            SecretKeySpec spec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, spec);
            BASE64Encoder enc = new BASE64Encoder();
            return enc.encode(cipher.doFinal(text.getBytes()));
        }
      
        public static String decrypt(String text) throws Exception {
            BASE64Decoder decoder = new BASE64Decoder();
            ResourceBundle bundle = ResourceBundle.getBundle("key");
            byte[] raw = decoder.decodeBuffer(bundle.getString("key"));
            SecretKeySpec spec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, spec);
            BASE64Decoder dec = new BASE64Decoder();
            return new String(cipher.doFinal(dec.decodeBuffer(text)));
        }
        
        public static void generateKey() {
            SecureRandom random = new SecureRandom();
            BASE64Encoder encoder = new BASE64Encoder();

            byte[] raw = random.generateSeed(16);
            String string = encoder.encode(raw);
            System.out.println(string);
        }

        
//  public static void main(String[] args) throws Exception {
//        
//
//	 String text = "zameer";
//         String encrypted = encrypt(text);
//	 System.out.println(encrypted);
//         
//         String decrypted = decrypt(encrypted);
//         System.out.println(decrypted);
//  }
}
